package org.opicasso.cheese.ui.components;

import java.text.NumberFormat;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.opicasso.cheese.domain.Cart;
import org.opicasso.cheese.domain.Cheese;

public class ShoppingCartPanel extends Panel {

	private static final long serialVersionUID = -658434293253278371L;
	private final Cart cart;

	public ShoppingCartPanel(String id, Cart cart) {
		super(id);
		this.cart = cart;
		// cart
		add(new ListView("cart", getCart().getCheeses()) {

			private static final long serialVersionUID = 1860607962753316352L;

			@Override
			protected void populateItem(ListItem item) {
				final Cheese cheese = (Cheese) item.getModelObject();
				item.add(new Label("name", cheese.getName()));
				item.add(new Label("price", "$" + cheese.getPrice()));
				item.add(new Link("remove") {
					private static final long serialVersionUID = -1719911741708764020L;

					@Override
					public void onClick() {
						getCart().getCheeses().remove(cheese);
					}
				});
			}
		});
		add(new Label("total", new Model() {
			private static final long serialVersionUID = 6999633557393712852L;

			@Override
			public Object getObject() {
				return NumberFormat.getCurrencyInstance().format(
						getCart().getTotal());
			}

		}));
	}

	public Cart getCart() {
		return cart;
	}

}
